package mapReduce.demo01_wordCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {

    Text outKey = new Text();
    IntWritable outValue = new IntWritable(1);

    int i=0;

    @Override
    protected void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        System.out.println("mapper=====setup");
    }

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

        System.out.println("第"+ ++i +"行的偏移量为："+key.get());

        //表示获取每行的内容，为了方便后面的处理，把Text类型转成相应的字符串类型
        String line = value.toString();
        //把每行的内容按照空格拆分成单词的数组
        String[] words = line.split(" ");

        //对每行的单词数组进行遍历，获取每个单词
        for (String word : words) {
            outKey.set(word);
            context.write(outKey,outValue);
        }

    }

    @Override
    protected void cleanup(Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        System.out.println("mapper=====cleanup");
    }
}
